﻿using System;
using System.ComponentModel;
using System.Drawing;
using System.IO;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using YArchitech.Controls;
using YJKArchUtils.Utils;
using YJKRevitFloorModule.InclineFloor;

namespace YJKRevitFloorModule.Forms
{
	public partial class InclineFloorForm : System.Windows.Forms.Form
	{
		public double ZOffset
		{
			get
			{
				double result;
				try
				{
					double num = Common.MMToFeet(Convert.ToDouble(this.tbValue.Text));
					this.m_ZOffset = this.tbValue.Text;
					result = num;
				}
				catch (Exception)
				{
					result = Common.MMToFeet(Convert.ToDouble(this.m_ZOffset));
				}
				return result;
			}
		}

		public double RotateAngle
		{
			get
			{
				double result;
				try
				{
					double num = Convert.ToDouble(this.tbValue.Text) / 180.0 * Math.PI;
					this.m_RotateAngle = this.tbValue.Text;
					result = num;
				}
				catch (Exception)
				{
					result = Convert.ToDouble(this.m_RotateAngle) / 180.0 * Math.PI;
				}
				return result;
			}
		}

		public bool IsSelEdgeIncline
		{
			get
			{
				return this.rbSelEdgeIncline.Checked;
			}
		}

		public InclineFloorForm()
		{
			this.InitializeComponent();
		}

		private void InclineFloorForm_Load(object sender, EventArgs e)
		{
			this.ReadData();
		}

		private void InclineFloorForm_FormClosed(object sender, FormClosedEventArgs e)
		{
			this.SetFocusToRevit();
			this.CancelRevitOperate();
		}

		private void InclineFloorForm_FormClosing(object sender, FormClosingEventArgs e)
		{
			this.SaveData();
		}

		private void InclineFloorForm_MouseLeave(object sender, EventArgs e)
		{
			if (!base.ClientRectangle.Contains(base.PointToClient(Cursor.Position)))
			{
				this.SetFocusToRevit();
				return;
			}
			base.Focus();
		}

		private void rbSelEdgeIncline_CheckedChanged(object sender, EventArgs e)
		{
			if (this.rbSelEdgeIncline.Checked)
			{
				if (this.IsValidatDouble(this.tbValue.Text))
				{
					this.m_RotateAngle = this.tbValue.Text;
				}
				this.lbPrompt.Text = "Z向偏移量(mm)";
				this.tbValue.Text = this.m_ZOffset;
				return;
			}
			if (this.IsValidatDouble(this.tbValue.Text))
			{
				this.m_ZOffset = this.tbValue.Text;
			}
			this.lbPrompt.Text = "旋转角度(°)";
			this.tbValue.Text = this.m_RotateAngle;
		}

		private bool IsValidatDouble(string strValue)
		{
			bool result;
			try
			{
				Convert.ToDouble(strValue);
				result = true;
			}
			catch (Exception)
			{
				result = false;
			}
			return result;
		}

		private void ReadData()
		{
			string path = Path.GetTempPath() + "InclineFloorForm.dat";
			if (File.Exists(path))
			{
				base.StartPosition = FormStartPosition.Manual;
				FileStream fileStream = new FileStream(path, FileMode.Open);
				StreamReader streamReader = new StreamReader(fileStream);
				streamReader.BaseStream.Seek(0L, SeekOrigin.Begin);
				int x = Convert.ToInt32(streamReader.ReadLine());
				int y = Convert.ToInt32(streamReader.ReadLine());
				base.Location = new System.Drawing.Point(x, y);
				this.m_ZOffset = streamReader.ReadLine();
				this.m_RotateAngle = streamReader.ReadLine();
				this.rbSelEdgeIncline.Checked = Convert.ToBoolean(streamReader.ReadLine());
				this.rbRotationByEdge.Checked = Convert.ToBoolean(streamReader.ReadLine());
				if (this.rbSelEdgeIncline.Checked)
				{
					this.tbValue.Text = this.m_ZOffset;
				}
				else
				{
					this.tbValue.Text = this.m_RotateAngle;
				}
				streamReader.Close();
				fileStream.Close();
				return;
			}
			base.StartPosition = FormStartPosition.CenterScreen;
			this.m_ZOffset = "500";
			this.m_RotateAngle = "30";
			this.rbSelEdgeIncline.Checked = true;
			this.rbRotationByEdge.Checked = false;
			if (this.rbSelEdgeIncline.Checked)
			{
				this.tbValue.Text = this.m_ZOffset;
				return;
			}
			this.tbValue.Text = this.m_RotateAngle;
		}

		private void SaveData()
		{
			FileStream fileStream = new FileStream(Path.GetTempPath() + "InclineFloorForm.dat", FileMode.Create);
			StreamWriter streamWriter = new StreamWriter(fileStream);
			streamWriter.WriteLine(base.Location.X);
			streamWriter.WriteLine(base.Location.Y);
			streamWriter.WriteLine(this.m_ZOffset);
			streamWriter.WriteLine(this.m_RotateAngle);
			streamWriter.WriteLine(this.rbSelEdgeIncline.Checked);
			streamWriter.WriteLine(this.rbRotationByEdge.Checked);
			streamWriter.Flush();
			streamWriter.Close();
			fileStream.Close();
		}

		[DllImport("user32.dll", EntryPoint = "SendMessageA")]
		public static extern int SendMessage(IntPtr hwnd, int wMsg, IntPtr wParam, IntPtr lParam);

		private void SetFocusToRevit()
		{
			int wMsg = 7;
			InclineFloorForm.SendMessage(CmdInclineFloor._hWndRevit.Handle, wMsg, IntPtr.Zero, IntPtr.Zero);
		}

		[DllImport("user32.dll")]
		public static extern void keybd_event(byte bVk, byte bScan, int dwFlags, int dwExtraInfo);

		private void CancelRevitOperate()
		{
			int bVk = 27;
			int dwFlags = 0;
			int dwFlags2 = 2;
			InclineFloorForm.keybd_event((byte)bVk, 0, dwFlags, 0);
			InclineFloorForm.keybd_event((byte)bVk, 0, dwFlags2, 0);
		}

		private void tbValue_TextChanged(object sender, EventArgs e)
		{
			if (this.tbValue.Text.CompareTo("-") == 0)
			{
				return;
			}
			if (this.rbSelEdgeIncline.Checked)
			{
				if (!Common.IsValidating(this.tbValue.Text, -5000.0, 5000.0))
				{
					YJKMessageBox.Show("合法范围在-5000mm～5000mm之间");
					this.tbValue.Text = this.m_ZOffset;
					return;
				}
				this.m_ZOffset = this.tbValue.Text;
				return;
			}
			else
			{
				if (!Common.IsValidating(this.tbValue.Text, -89.0, 89.0))
				{
					YJKMessageBox.Show("合法范围在-89°～89°之间");
					this.tbValue.Text = this.m_RotateAngle;
					return;
				}
				this.m_RotateAngle = this.tbValue.Text;
				return;
			}
		}

		private string m_ZOffset = "0.0";

		private string m_RotateAngle = "0.0";
	}
}
